package com.cedifie.lendapp.utils.dialog

import android.Manifest
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.cedifie.lendapp.R
import com.cedifie.lendapp.databinding.DialogPhotoBinding
import com.cedifie.lendapp.http.HttpHelper
import com.cedifie.lendapp.utils.event.CedEventType
import com.cedifie.lendapp.utils.helper.PermissionHelper
import com.cedifie.lib_permission.OnPermissionCallback
import com.cedifie.lib_permission.XXPermissions
import org.json.JSONObject

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2024/6/20 17:59
 * @Annotation：拍照、相册选择 dialog
 */
class PhotoDialog: ProDialog {
    private lateinit var binding: DialogPhotoBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogPhotoBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.imgClose) //关闭
        setOnClickListener(R.id.cameraLayout) //拍照
        setOnClickListener(R.id.albumLayout) //相册选择
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        when(v?.id){
            R.id.cameraLayout -> {
                onCedCameraClick()
            }
            R.id.albumLayout -> {
                mCallback?.openAlbum()
            }
        }
    }

    /**
     * 拍照
     */
    private fun onCedCameraClick(){
        val permissions = arrayListOf(Manifest.permission.CAMERA)
        if (XXPermissions.isGranted(context,permissions)){
            mCallback?.openCamera()
        } else {
            val cameraItem = PermissionHelper.getCedCameraPermissionItem()
            showCedCameraReasonDialog(cameraItem)
        }
    }

    /**
     * 相机权限说明
     */
    private fun showCedCameraReasonDialog(item: JSONObject){
        val permission = item.optString("permission")
        val reasonDialog = ReasonDialog(context,permission,object: ReasonDialog.IDialogCallback{
            override fun onReasonDenyClick() {
                HttpHelper.addCedEventAction(CedEventType.CLICK.CED_CLICK_PERMISSION_DENY,permission)
            }
            override fun onReasonAgreeClick() {
                HttpHelper.addCedEventAction(CedEventType.CLICK.CED_CLICK_PERMISSION_AGREE,permission)
                requestCedCameraPermission(item)
            }
        })
        reasonDialog.setCancelable(true)
        reasonDialog.setCanceledOnTouchOutside(true)
        reasonDialog.show()
        reasonDialog.showCedPermissionItemViews(item)
    }

    /**
     * 授权
     */
    private fun requestCedCameraPermission(item: JSONObject){
        val permission = item.optString("permission")
        XXPermissions.with(context)
            // 申请多个权限
            .permission(permission)
            .request(object: OnPermissionCallback {
                override fun onGranted(p0: MutableList<String>, p1: Boolean) {
                    var permissionString = ""
                    p0.forEach {
                        permissionString = "$permissionString$it,"
                    }
                    HttpHelper.addCedEventAction(CedEventType.CLICK.CED_CLICK_PERMISSION_SYSTEM_AGREE,permissionString.substring(0,permissionString.length - 1))
                    mCallback?.openCamera()
                }
                override fun onDenied(p0: MutableList<String>, doNotAskAgain: Boolean) {
                    var permissionString = ""
                    p0.forEach {
                        permissionString = "$permissionString$it,"
                    }
                    HttpHelper.addCedEventAction(CedEventType.CLICK.CED_CLICK_PERMISSION_SYSTEM_DENY,permissionString.substring(0,permissionString.length - 1))
                    XXPermissions.startPermissionActivity(context,permission)
                }
            })
    }

    interface IDialogCallback {
        //打开相机
        fun openCamera()
        //打开相册
        fun openAlbum()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addCedEventAction(CedEventType.DIALOG.CED_DIALOG_PHOTO_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addCedEventAction(CedEventType.DIALOG.CED_DIALOG_PHOTO_DISMISS,eventRemark)
    }
}